<template>
  <div id="app" class="skStudentTable">
    <a-table :loading="loading" :columns="columns" :data-source="dataSource" rowKey="studentId" bordered :scroll="{x:columns.length > 6?1800:0,y:800}"></a-table>
  </div>
</template>
<script>
import { getAction } from '@/api/manage'
export default {
  name: 'skStudentTable',
  data() {
    return {
      url: {
        list: "/health/skBmiRecord/queryBmi"
      },
      loading:false,
      dataDate: '',
      columns:[],
      dataSource:[],
      dateType:"week",
    }
  },
  props:["detailName"],
  components: {},

  filters: {},

  computed: {},
  
  created() {
    this.dataDate = this.$skCommon.getDate(1);
  },
  
  activated() {},
  
  mounted() {},
  
  methods: {
    // 获取学生具体信息
    getStudentData(dataForm, months){     
      console.log('dataForm', dataForm)
      this.setColumns(months);

      const params = {
        statisticalScope: 'student',
        semesterId: dataForm.semesterId ? dataForm.semesterId : dataForm.semesterHistoryId,
        classId: dataForm.classId ? dataForm.classId : undefined
      }

      this.loading = true;
      getAction(this.url.list,params).then((res)=>{
        if(res.success){
          this.dataSource = [];
          if(res.result.length > 0) {
            let studentList = this.unique(res.result.map(item => (
              {
                classId: item.classId,
                className: item.className,
                classType: item.classType,
                studentId: item.studentId,
                studentName: item.studentName
              }
            )), 'studentId');
            console.log('studentList', studentList);
            studentList.forEach(el => {
              let stuItem = el;

              const stuData = res.result.filter(item => {
                return item.studentId == el.studentId
              })

              for(let rowIndex = 0; rowIndex < months.length; rowIndex++){
                const rowString = months[rowIndex].toString()
                const monthData = stuData.find(item => (item.year + '-' + item.month) == rowString);
                if(monthData){
                  stuItem['avgHeight' + rowString] = monthData.height;
                  stuItem['avgWeight' + rowString] = monthData.weight;
                }else{
                  stuItem['avgHeight' + rowString] = '-';
                  stuItem['avgWeight' + rowString] = '-';
                }
              }
              
              this.dataSource.push(stuItem);
            })
          }
          this.loading = false;
        } else {
          this.$message.warning(res.message);
        }
      }).catch((error)=>{
        console.error(error);
        this.loading = false;
      })
    },
    unique(newarr, key) {
      const res = new Map();
      return newarr.filter((arr) => !res.has(arr[key]) && res.set(arr[key], 1));
    },
    // 表头
    setColumns(months){
      this.columns = [
        {
          title: '年级',
          align: 'center',
          fixed: 'left',
          width: 80,
          dataIndex: 'classType'
        },
        {
          title: '班级',
          align: 'center',
          dataIndex: 'className',
          fixed: 'left',
          width: 80,
        },
        {
          title: '学生',
          align: 'center',
          dataIndex: 'studentName',
          fixed: 'left',
          width: 80,
        }
      ];
      for(let rowIndex = 0; rowIndex < months.length; rowIndex++){
        const rowString = months[rowIndex].toString()
        this.columns.push(
          {
            title: rowString,
            children: [
              {
                title: '身高(cm)',
                dataIndex: 'avgHeight'+rowString,
                key: 'avgHeight'+rowString,
                width: months.length > 6 ? 100 : 'unset',
              },
              {
                title: '体重(kg)',
                dataIndex: 'avgWeight'+rowString,
                key: 'avgWeight'+rowString,
                width: months.length > 6 ? 100 : 'unset',
              },
            ],
          },
        )
      }
    },
  }
}
</script>
<style scoped>
/* @import url(); 引入css类 */
</style>
